package com.dashan.backend.datastore;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.dashan.backend.XmlDeserializable;
import com.dashan.backend.XmlSerializable;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class DsWork implements XmlSerializable, XmlDeserializable {
    @SuppressWarnings("unused")
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Long id;
    
    @Persistent
    private String companyName = "";

    @Persistent
    private String description = "";
    
    @Persistent
    private String position = "";
    
    @Persistent
    /** yyyy-mm-dd */
    private String start;
    
    @Persistent
    /** yyyy-mm-dd */
    private String end;

    public DsWork() {
    }
    
    /**
     * @param start yyyy-mm-dd
     * @param end yyyy-mm-dd
     */
    public DsWork(String companyName, String description, String position, String start, String end) {
        this.companyName = companyName;
        this.description = description;
        this.position = position;
        this.start = start;
        this.end = end;
    }
    public String getCompanyName() {
        return companyName;
    }
    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public String getPosition() {
        return position;
    }
    public void setPosition(String position) {
        this.position = position;
    }
    public String getStart() {
        return start;
    }
    public void setStart(String start) {
        this.start = start;
    }
    public String getEnd() {
        return end;
    }
    public void setEnd(String end) {
        this.end = end;
    }
    
    @Override
    public Element getXmlElement(Document xmlDoc) {
        Element root = xmlDoc.createElement("work");
        Element ele = null;
        
        ele = xmlDoc.createElement("companyName");
        ele.appendChild(xmlDoc.createTextNode(companyName));
        root.appendChild(ele);

        ele = xmlDoc.createElement("description");
        ele.appendChild(xmlDoc.createTextNode(description));
        root.appendChild(ele);
        
        ele = xmlDoc.createElement("position");
        ele.appendChild(xmlDoc.createTextNode(position));
        root.appendChild(ele);
        
        if (start != null) {
        ele = xmlDoc.createElement("start");
            ele.appendChild(xmlDoc.createTextNode(start.toString()));
        root.appendChild(ele);
        }

        if (end != null) {
        ele = xmlDoc.createElement("end");
            ele.appendChild(xmlDoc.createTextNode(end.toString()));
        root.appendChild(ele);
        }
        
        return root;
    }
    
    @Override
    public void fromXmlElement(Element ele) {
        NodeList e = ele.getElementsByTagName("companyName");
        if (e.getLength() != 0 && e.item(0).getFirstChild() != null) {
            companyName = e.item(0).getFirstChild().getNodeValue();
        }
        e = ele.getElementsByTagName("description");
        if (e.getLength() != 0 && e.item(0).getFirstChild() != null) {
            description = e.item(0).getFirstChild().getNodeValue();
        }
        e = ele.getElementsByTagName("position");
        if (e.getLength() != 0 && e.item(0).getFirstChild() != null) {
            position = e.item(0).getFirstChild().getNodeValue();
        }
        e = ele.getElementsByTagName("start");
        if (e.getLength() != 0 && e.item(0).getFirstChild() != null) {
            start = e.item(0).getFirstChild().getNodeValue();
        }
        e = ele.getElementsByTagName("end");
        if (e.getLength() != 0 && e.item(0).getFirstChild() != null) {
            end = e.item(0).getFirstChild().getNodeValue();
        }
    }
    
    @Override
    public String toString() {
        return String.format("%s, %s, %s, (%s-%s)", companyName, description, position, start, end);
    }
}
